package com.example.shardingAlgorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.time.LocalDate;
import java.util.Collection;

public class BirthdayAlgorithm  implements PreciseShardingAlgorithm<String> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        String birthday = preciseShardingValue.getValue();

        String month = String.format("%02d", LocalDate.parse(birthday).getMonth().getValue());

        String table = preciseShardingValue.getLogicTableName() + month;
        for (String dataNode : collection) {
            if (dataNode.equals(table)) {
                return dataNode;
            }
        }

        return null;
    }
}
